package info.motteke.qjdbc.db;

import info.motteke.qjdbc.exception.QuickJDBCException;
import info.motteke.qjdbc.exception.SqlException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public final class JdbcConnectionFactory extends ConnectionFactory {
    private final String url;

    public JdbcConnectionFactory(String driver, String url) {
        this(driver, url, null, null);
    }

    public JdbcConnectionFactory(String driver, String url, String user, String password) {
        super(user, password);
        if (driver == null || url == null) {
            throw new IllegalArgumentException();
        }
        try{
            Class.forName(driver);
        } catch (ClassNotFoundException e){
            throw new QuickJDBCException(e);
        }

        this.url = url;
    }

    @Override
    protected Connection getConnection() {
        try {
            return DriverManager.getConnection(url);
        } catch (SQLException e) {
            throw new SqlException(e);
        }
    }

    @Override
    protected Connection getConnection(String user, String password){
        try{
            return DriverManager.getConnection(password, user, password);
        } catch (SQLException e) {
            throw new SqlException(e);
        }
    }
}
